package com.myamap.tool;

import org.CrossApp.lib.CrossAppActivity;

import android.os.Bundle;
import android.util.SparseArray;

/*
 * 高德2D地图帮助类
 * 
 */
public class MyAmapViewHelper 
{
	private static SparseArray<MyAmapView> s_AmapViews = new SparseArray<MyAmapView>();

	private static int viewTag = 0;
	
	private static Bundle mBundle = null;
	
	//地图加载成功
	public native static void onAmapLoaded(int tag);
	//地图点击回调
	public native static void onAmapClicked(int tag, double lat, double lng);
	//地图marker拖拽回调
	public native static void onAmapMarkerDragStart(int tag, String markertag, double lat, double lng);
	public native static void onAmapMarkerDrag(int tag, String markertag, double lat, double lng);
	public native static void onAmapMarkerDragEnd(int tag, String markertag, double lat, double lng);
	//地图marker点击事件
	public native static void onAmapMarkerClick(int tag, String markertag);
	//地图marker的InfoWindow点击
	public native static void onAmapMarkerInfoWindowClick(int tag, String markertag);
	//地图右上角定位按钮
	public native static void onAmapLocationEnd(int tag, int code, double lat, double lng, String formatAddr, 
			String province, String city, String district, String street, String number, String poi, String adcode);
	
	//驾车路线规划
	public native static void onDriveRouteSearched(int tag, int code, double slat, double slng, double elat, double elng, int sumtime, int sumdistance, float taxicost, String discription, 
			float[] distanceArray, float[] durationArray, String[] instructArray, String[] orientationArray);
	
	//步行路线规划
	public native static void onWalkRouteSearched(int tag, int code, double slat, double slng, double elat, double elng, int sumtime, int sumdistance, String discription, 
			float[] distanceArray, float[] durationArray, String[] instructArray, String[] orientationArray);
	
	//公交路线规划
	public native static void onBusRouteSearched(int tag, int code, double[] slatArray, double[] slngArray, double[] elatArray, double[] elngArray, int[] tagArray, int[] distanceArray, int[] durationArray, float[] costArray, 
			String[] pathTitleArray, String[] pathDesArray, float[] busdisArray, float[] walkdisArray);
	
	//公交步骤
	public native static void onBusRouteSteps(int tag, int code, int bustag, int[] typeArray, Object[] nameArray, float[] distanceArray, float[] durationArray,	Object[] stationsArray);
	
	public static void  initBundle(Bundle bundle)
	{
		mBundle = bundle;
	}
	
	public static int createAMap(float x, float y, float width, float height) 
    {
        final int index = viewTag;
        createAMap(index, x, y, width, height);
        return viewTag++;
    }
    
	public static void createAMap(final int index, final float x, final float y, final float width, final float height)
	{
        CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
            	MyAmapView mymapView = new MyAmapView(CrossAppActivity.getContext(), index, mBundle, x, y, width, height);
            	s_AmapViews.put(index, mymapView);
            }
        });
    }
	
    public static void setVisible(final int index, final boolean visible) 
    {
    	CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
		    	MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setVisible(visible);	
		    	}
            }
        });
    }
	
	public static void destroyAMap(final int index)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
            	MyAmapView myAmapView = s_AmapViews.get(index);
            	if (myAmapView != null)
            	{
            		s_AmapViews.remove(index);
            		myAmapView.destroy();
            	}
            }
        });
	}
	
	public static void setMapCenter(final int index, final double lat, final double lng)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setMapCenter(lat, lng);
		    	}
            }
        });
	}
	
	public static void startLocation(final int index)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
            	MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.startLocation();
		    	}
            }
        });
	}
	
	public static void setMapZoomLevel(final int index, final double level)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setMapZoomLevel(level);
		    	}
            }
        });
	}
	
	public static void addMarker(final int tag, final String marktag, final String img, final boolean dragable, final String title, final String content, final double lat, final double lng)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(tag);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.addMarker(marktag, img, dragable, title, content, lat, lng);
		    	}
            }
        });
	}
	
	public static void deleteMarker(final int tag, final String marktag)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(tag);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.deleteMarker(marktag);
		    	}
            }
        });
	}
	
	public static void clearMarkers(final int tag)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(tag);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.clearMarkers();
		    	}
            }
        });
	}
	
	public static void setRouteViewType(final int index, final double slat, final double slng, final double elat, final double elng, final int value)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setRouteViewType(slat, slng, elat, elng, value);	
		    	}
            }
        });
	}
	
	public static void searchRoute(final int index)
	{
    	CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
		    	MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.searchRoute();
		    	}
		    }
        });
	}
	
	public static void setBusRouteCity(int index, String city, String cityd)
	{
		MyAmapView myAmapView = s_AmapViews.get(index);
    	if (myAmapView != null)
    	{
    		myAmapView.setBusRouteCity(city, cityd);
    	}
	}
	
	public static void showBusRouteView(final int index, final int bustag)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
		    	MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.showBusRouteView(bustag);
		    	}
		    }
        });
	}
	
	public static void getBusRouteStep(final int index, final int bustag)
	{
		MyAmapView myAmapView = s_AmapViews.get(index);
    	if (myAmapView != null)
    	{
    		myAmapView.getBusRouteStep(bustag);
    	}
	}
	
	public static void setAMapRect(final int index, final int x, final int y, final int width, final int height)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setAMapRect(x, y, width, height);
		    	}
            }
        });
	}
}
